/*global nw:true, window */

"use strict";

window.nw = window.nw || {};

nw.Core = function() {
    this._modules = {};
    this._plugins = {};
    this._sandbox = null;
	this.currentViewName = nw.enums.views.v1;
};

nw.Core.prototype = (function() {
    function isFunction(obj) {
        return typeof obj === 'function';
    }

    return {
        _startModules: function() {
            for (var name in this._modules) {
                if(this._modules.hasOwnProperty(name)) {
                    var module = this._modules[name];
                    module.instance = new module.constructor(this._sandbox);

                    // initialize the module
                    if (isFunction(module.instance.init)) {
                        module.instance.init();
                    }
                }
            }
        },
        _renderModules: function() {
            for (var name in this._modules) {
                if(this._modules.hasOwnProperty(name)) {
                    var module = this._modules[name];

                    // initialize the module
                    if (module.instance && isFunction(module.instance.render)) {
                        module.instance.render();
                    }
                }
            }
        },
        init: function(sbox) {
            this._sandbox = sbox;
            this._startModules();
            this._renderModules();
        },
        loadModule: function(moduleName, fn) {
            this._modules[moduleName] = {
                constructor: fn,
                instance: null
            };
        },
        getModule: function(moduleName) {
            return this._modules[moduleName];
        },
        loadPlugin: function(pluginName, plugin) {
            this._plugins[pluginName] = plugin;
        },
        getPlugin: function(pluginName) {
            return this._plugins[pluginName];
        },
        views: function() {
            return nw.enums.views;
        },
        goView: function(viewName) {
            var currentView = this.getModule(this.currentViewName);
            this.currentViewName = viewName;
            currentView.instance.hide();
            var view = this.getModule(viewName);
            view.instance.show();
        }
    };
})();
